package cn.touna.jss.modules.credit.service.impl;

import cn.touna.jss.common.entity.CreditQueryRequest;
import cn.touna.jss.common.util.DateUtils;
import cn.touna.jss.modules.credit.entity.CreditQueryHistoryEntity;
import cn.touna.jss.modules.credit.entity.CreditQueryHistoryEntityExample;
import cn.touna.jss.modules.credit.entity.CreditQueryHistorySimplifiedEntity;
import cn.touna.jss.modules.credit.entity.CreditRecordRef;
import cn.touna.jss.modules.credit.mapper.CreditQueryHistoryEntityMapper;
import cn.touna.jss.modules.credit.mapper.CreditRecordRefMapper;
import cn.touna.jss.modules.credit.mapper.NickNameEntityMapper;
import cn.touna.jss.modules.credit.service.CreditQueryService;
import cn.touna.risk.api.domain.Response;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 接口查询记录
 * Created by wuwc on 2017/7/28.
 */
@Service("creditQueryService")
public class CreditQueryServiceImpl implements CreditQueryService {

    private final static Logger logger = LoggerFactory.getLogger(CreditQueryServiceImpl.class);
    @Autowired
    private CreditQueryHistoryEntityMapper creditQueryHistoryMapper;

    @Autowired
    private CreditRecordRefMapper creditRecordRefMapper;

    @Autowired
    private NickNameEntityMapper nickNameEntityMapper;

    public List<CreditQueryHistoryEntity> selectByCrNo(String crNo){
        CreditQueryHistoryEntityExample example = new CreditQueryHistoryEntityExample();
        example.createCriteria().andCrNoEqualTo(crNo);
        return creditQueryHistoryMapper.selectByExample(example);
    }

    public List<CreditQueryHistoryEntity> selectByMethod(String serviceName,String methodName,Date begin,Date end){
        CreditQueryHistoryEntityExample example = new CreditQueryHistoryEntityExample();
        example.createCriteria()
                .andServiceNameEqualTo(serviceName)
                .andmethodNameEqualTo(methodName)
                .andDateCreatedGreaterThan(begin)
                .andDateCreatedLessThan(end);
        return creditQueryHistoryMapper.selectByExample(example);
    }

    @Override
    public void saveQueryHistory(CreditQueryRequest request, Response creditQueryResponse) {
        try {
            CreditQueryHistoryEntity creditQueryHistoryEntity = new CreditQueryHistoryEntity();
            creditQueryHistoryEntity.setCrNo(request.getCrNo());
            creditQueryHistoryEntity.setBizType(request.getBizType());
            creditQueryHistoryEntity.setSysName(request.getSysName());
            creditQueryHistoryEntity.setProductType(request.getProductType());
            creditQueryHistoryEntity.setPhaseName(request.getPhaseName());
            creditQueryHistoryEntity.setApplyNoType(request.getApplyNoType());
            creditQueryHistoryEntity.setApplyNo(request.getApplyNo());
            creditQueryHistoryEntity.setIdNo(request.getIdCard());
            creditQueryHistoryEntity.setMobile(request.getMobile());
            creditQueryHistoryEntity.setMethodName(request.getMethodName());
            creditQueryHistoryEntity.setServiceName(request.getServiceName());
            creditQueryHistoryEntity.setSrc(creditQueryResponse.getSrc());
            creditQueryHistoryEntity.setParams(JSON.toJSONString(request.getParams()));
            creditQueryHistoryEntity.setTimeQuery(request.getQueryTime());
            creditQueryHistoryEntity.setResponseCode(creditQueryResponse.getCode());
            creditQueryHistoryEntity.setResponseMsg(creditQueryResponse.getMsg());
            creditQueryHistoryEntity.setClientIp(request.getClientIp());
            creditQueryHistoryEntity.setTimeResponse(DateUtils.transferLongToDate(DateUtils.YYYY_MM_DD_HH_MM_SS, creditQueryResponse.getRespTimestamp()));
            creditQueryHistoryMapper.insertSelective(creditQueryHistoryEntity);
            logger.info("【调用历史入库成功】,method:{}", request.getMethod());
        } catch (Exception e) {
            logger.info("【调用历史入库异常】,method:{}，msg:{}", request.getMethod(), e.getMessage());
        }
    }

    @Override
    public List<CreditQueryHistorySimplifiedEntity> queryCreditHistorySimplified(CreditQueryHistoryEntity entity){
        if (entity == null)
            return null;
        CreditQueryHistoryEntityExample example = new CreditQueryHistoryEntityExample();
        CreditQueryHistoryEntityExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(entity.getIdNo()))
            criteria.andIdNoEqualTo(entity.getIdNo());
        if (!StringUtils.isEmpty(entity.getMobile()))
            criteria.andMobileEqualTo(entity.getMobile());
        if (!StringUtils.isEmpty(entity.getResponseCode()))
            criteria.andResponseCodeEqualTo(entity.getResponseCode());
        return creditQueryHistoryMapper.selectBySimplifiedExample(example);
    }

    @Override
    public void saveCreditRef(CreditRecordRef recordRef) {
        creditRecordRefMapper.insertSelective(recordRef);
    }

    @Override
    public List<String> selectNiceList(String type) {
        return nickNameEntityMapper.selectNiceList(type);
    }

}
